package com.gd.iot.energy.service.systemadmin.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gd.iot.energy.code.ResponseCode;
import com.gd.iot.energy.consts.SystemConsts;
import com.gd.iot.energy.dao.systemadmin.EquipConcentPOMapper;
import com.gd.iot.energy.exception.MyException;
import com.gd.iot.energy.po.systemadmin.EquipConcentPO;
import com.gd.iot.energy.pojo.common.vo.ColumnVO;
import com.gd.iot.energy.pojo.common.vo.PageVO;
import com.gd.iot.energy.pojo.systemadmin.dto.EquipConcentSaveDTO;
import com.gd.iot.energy.pojo.systemadmin.dto.EquipmentmodelSaveDTO;
import com.gd.iot.energy.pojo.systemadmin.vo.EnumVO;
import com.gd.iot.energy.pojo.systemadmin.vo.EquipConcentPageVO;
import com.gd.iot.energy.pojo.systemadmin.vo.EquipConcentVO;
import com.gd.iot.energy.service.systemadmin.AreaOrganizationService;
import com.gd.iot.energy.service.systemadmin.EquipConcentService;
import com.gd.iot.energy.util.CustomBeanUtils;
import com.gd.iot.energy.util.CustomColumnUtil;
import com.gd.iot.energy.util.CustomStrUtil;
import com.gd.iot.energy.util.EnergyTypeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 集中器
 *
 * @author kris
 * @date Created at 8:33 下午 2020/8/5
 */
@Service
public class EquipConcentServiceImpl extends ServiceImpl<EquipConcentPOMapper, EquipConcentPO> implements EquipConcentService {

    @Autowired
    private AreaOrganizationService areaOrganizationService;

    @Autowired
    private EquipmentmodelServiceImpl equipmentmodelService;

    @Resource
    private EquipConcentPOMapper equipConcentPOMapper;

    /**
     * column 文件
     */
    private static final String EQUIP_CONCENT_LIST_COLUMN_FILE_NAME = "systemadmin/equip_concent_list.column";

    @Override
    public List<EquipConcentVO> getEquipConcentVOs(Integer areaOrg, String conncentName) {
        QueryWrapper<EquipConcentPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", SystemConsts.BooleanEnum.FALSE.value);
        if (areaOrg != null) {
            List<Long> allChildAreaIds = areaOrganizationService.getAllChildAreaIds(areaOrg);
            if (CollUtil.isNotEmpty(allChildAreaIds)) {
                queryWrapper.in("area_org", allChildAreaIds);
            }
        }
        if (StrUtil.isNotBlank(conncentName)) {
            queryWrapper.like("conncent_name", conncentName);
        }
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        List<EquipConcentPO> concentPOS = equipConcentPOMapper.selectList(queryWrapper);
        List<EquipConcentVO> concentVOS = new ArrayList<>(concentPOS.size());
        CustomBeanUtils.convertPojo(concentPOS,concentVOS,EquipConcentVO.class);
        return concentVOS;
    }

    @Override
    public PageVO<EquipConcentPageVO> getPageList(String conncentSerialnumber, String conncentName,
                                                  Integer areaId, Integer pageSize, Integer pageNum,
                                                  String orderColumn, String orderType) {
        IPage<EquipConcentPO> page = new Page<>(pageNum, pageSize);
        QueryWrapper<EquipConcentPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", SystemConsts.BooleanEnum.FALSE.value);
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        if (StrUtil.isNotBlank(conncentSerialnumber)) {
            queryWrapper.like("conncent_serialnumber", conncentSerialnumber);
        }
        if (StrUtil.isNotBlank(conncentName)) {
            queryWrapper.like("conncent_name", conncentName);
        }

        if (areaId != null) {
            List<Long> allChildAreaIds = areaOrganizationService.getAllChildAreaIds(areaId);
            if (CollUtil.isNotEmpty(allChildAreaIds)) {
                queryWrapper.in("area_org", allChildAreaIds);
            }
        }

        if (StrUtil.isNotBlank(orderColumn) && StrUtil.isNotBlank(orderType)) {
            orderColumn = CustomStrUtil.humpToLine(orderColumn);
            queryWrapper.orderBy(true, orderType.equals(SystemConsts.OrderType.ASC.value), orderColumn);
        }

        IPage<EquipConcentPO> equipConcentPOPage = equipConcentPOMapper.selectPage(page, queryWrapper);
        PageVO<EquipConcentPageVO> pageVO = new PageVO<>();
        pageVO.setPageSize(pageSize);
        pageVO.setPageNum(pageNum);
        List<EquipConcentPageVO> equipCollectPageVOS = convertToEquipConcentPageVOList(equipConcentPOPage.getRecords(), pageSize, pageNum);
        pageVO.setData(equipCollectPageVOS);
        pageVO.setTotal(equipConcentPOPage.getTotal());
        pageVO.setPages(equipConcentPOPage.getPages());

        //表头
        List<ColumnVO> columns = CustomColumnUtil.getColumn(EQUIP_CONCENT_LIST_COLUMN_FILE_NAME);
        pageVO.setColumns(columns);
        return pageVO;
    }

    private List<EquipConcentPageVO> convertToEquipConcentPageVOList(List<EquipConcentPO> records, Integer pageSize, Integer pageNo) {
        List<EquipConcentPageVO> res = new ArrayList<>(records.size());
        EquipConcentPageVO concentPageVO;

        int i = pageSize * (pageNo - 1) + 1;
        for (EquipConcentPO record : records) {
            concentPageVO = new EquipConcentPageVO();
            concentPageVO.setSerialNum((long) i);
            concentPageVO.setId(record.getId());
            concentPageVO.setConncentName(record.getConncentName());
            concentPageVO.setConncentSerialnumber(record.getConncentSerialnumber());
            concentPageVO.setAreaOrgName(record.getConncentName());
            concentPageVO.setEquipmentTypeName(SystemConsts.EquipmentType.getDescByValue(record.getEquipmentType()));
            concentPageVO.setEquipmentAgreeName(SystemConsts.EquipmentAgreeEnum.getDescByValue(record.getEquipmentAgree()));
            concentPageVO.setEquipmentStateName(SystemConsts.RunStateEnum.getDescNameByValue(record.getEquipmentState()));
            concentPageVO.setEquipmentUpsideName(SystemConsts.EquipmentUpsideEnum.getDescNameByValue(record.getEquipmentUpside()));
            concentPageVO.setEquipmentDownsideName(SystemConsts.EquipmentDownEnum.getDescNameByValue(record.getEquipmentDownside()));
            concentPageVO.setEquipmentConnectTypeName(SystemConsts.EquipmentConnectType.getDescNameByValue(record.getEquipmentConnectType()));
            concentPageVO.setConcentStateName(null);
            i++;

            res.add(concentPageVO);
        }
        return res;
    }

    @Override
    public Long add(EquipConcentSaveDTO equipConcentSaveDTO) {
        if (equipConcentSaveDTO.getConncentName() == null) {
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        EquipConcentPO equipConcentPO = new EquipConcentPO();
        equipConcentPO.setCompanyId(0L);
        equipConcentPO.setCreateDate(new Date());
        equipConcentPO.setEnergyType(SystemConsts.EnergyTypeEnum.WATER.value);
        equipConcentPO.setIsDel(SystemConsts.BooleanEnum.FALSE.value);
        equipConcentPO.setModifyDate(new Date());
        equipConcentPO.setOperatorId(1L);

        equipConcentPO.setConncentName(equipConcentSaveDTO.getConncentName());
        equipConcentPO.setConnectSimCard(equipConcentSaveDTO.getConnectSimCard());
        equipConcentPO.setConncentSerialnumber(equipConcentSaveDTO.getConnectSimCard());
        equipConcentPO.setEquipmentAgree(equipConcentSaveDTO.getEquipmentAgree());
        equipConcentPO.setEquipmentConnectType(equipConcentSaveDTO.getEquipmentConnectType());
        equipConcentPO.setEquipmentDownside(equipConcentSaveDTO.getEquipmentDownside());
        equipConcentPO.setEquipmentState(equipConcentSaveDTO.getEquipmentState());
        equipConcentPO.setEquipmentUpside(equipConcentSaveDTO.getEquipmentUpside());
        equipConcentPO.setHandlingDate(equipConcentSaveDTO.getHandlingDate());
        equipConcentPO.setInstallationDate(equipConcentSaveDTO.getInstallationDate());
        equipConcentPO.setIsVirtual(equipConcentSaveDTO.getIsVirtual());
        equipConcentPO.setNormalDayDosage(equipConcentSaveDTO.getNormalDayDosage());
        equipConcentPO.setAreaOrg(equipConcentSaveDTO.getAreaOrg());
        equipConcentPO.setSysconfigSupplier(equipConcentSaveDTO.getSysconfigSupplier());

        equipConcentPO.setSysconfigEquipmentModel(equipConcentSaveDTO.getSysconfigEquipmentModel());
        EquipmentmodelSaveDTO equipmentmodel = equipmentmodelService.getEquipmentmodel(
                BigDecimal.valueOf(equipConcentSaveDTO.getSysconfigEquipmentModel()).intValue());

        equipConcentPO.setEquipmentType(equipmentmodel.getEquipmentType());
        equipConcentPO.setEquipmentnbtype(equipmentmodel.getEquipmentnbtype());

        equipConcentPO.setMemo(null);


        equipConcentPOMapper.insert(equipConcentPO);
        return equipConcentPO.getId();
    }

    @Override
    public boolean updateConcent(EquipConcentSaveDTO equipConcentSaveDTO) {
        if (equipConcentSaveDTO.getConncentName() == null) {
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        EquipConcentPO equipConcentPO = new EquipConcentPO();
        equipConcentPO.setId(equipConcentSaveDTO.getId());
        equipConcentPO.setModifyDate(new Date());
        equipConcentPO.setOperatorId(1L);

        equipConcentPO.setConncentName(equipConcentSaveDTO.getConncentName());
        equipConcentPO.setConnectSimCard(equipConcentSaveDTO.getConnectSimCard());
        equipConcentPO.setConncentSerialnumber(equipConcentSaveDTO.getConnectSimCard());
        equipConcentPO.setEquipmentAgree(equipConcentSaveDTO.getEquipmentAgree());
        equipConcentPO.setEquipmentConnectType(equipConcentSaveDTO.getEquipmentConnectType());
        equipConcentPO.setEquipmentDownside(equipConcentSaveDTO.getEquipmentDownside());
        equipConcentPO.setEquipmentState(equipConcentSaveDTO.getEquipmentState());
        equipConcentPO.setEquipmentUpside(equipConcentSaveDTO.getEquipmentUpside());
        equipConcentPO.setHandlingDate(equipConcentSaveDTO.getHandlingDate());
        equipConcentPO.setInstallationDate(equipConcentSaveDTO.getInstallationDate());
        equipConcentPO.setIsVirtual(equipConcentSaveDTO.getIsVirtual());
        equipConcentPO.setNormalDayDosage(equipConcentSaveDTO.getNormalDayDosage());
        equipConcentPO.setAreaOrg(equipConcentSaveDTO.getAreaOrg());
        equipConcentPO.setSysconfigSupplier(equipConcentSaveDTO.getSysconfigSupplier());

        equipConcentPO.setSysconfigEquipmentModel(equipConcentSaveDTO.getSysconfigEquipmentModel());
        EquipmentmodelSaveDTO equipmentmodel = equipmentmodelService.getEquipmentmodel(
                BigDecimal.valueOf(equipConcentSaveDTO.getSysconfigEquipmentModel()).intValue());

        equipConcentPO.setEquipmentType(equipmentmodel.getEquipmentType());
        equipConcentPO.setEquipmentnbtype(equipmentmodel.getEquipmentnbtype());

        equipConcentPO.setMemo(null);

        equipConcentPOMapper.updateById(equipConcentPO);
        return true;
    }

    @Override
    public boolean delete(List<Integer> ids) {
        if(CollUtil.isEmpty(ids)){
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        //供应商表逻辑删除
        QueryWrapper<EquipConcentPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        EquipConcentPO equipConcentPO = new EquipConcentPO();
        equipConcentPO.setIsDel(SystemConsts.BooleanEnum.TRUE.value);
        int num = equipConcentPOMapper.update(equipConcentPO,queryWrapper);

        return num > 0;
    }

    @Override
    public EquipConcentSaveDTO get(Integer id) {
        EquipConcentPO equipConcentPO = equipConcentPOMapper.selectById(id);
        if(equipConcentPO==null||SystemConsts.BooleanEnum.TRUE.value.equals(equipConcentPO.getIsDel())){
            throw new MyException(ResponseCode.RECORD_NOT_EXIST);
        }

        EquipConcentSaveDTO equipConcentSaveDTO = new EquipConcentSaveDTO();
        equipConcentSaveDTO.setId(equipConcentPO.getId());
        equipConcentSaveDTO.setConnectSimCard(equipConcentPO.getConnectSimCard());
        equipConcentSaveDTO.setConncentName(equipConcentPO.getConncentName());
        equipConcentSaveDTO.setAreaOrg(equipConcentPO.getAreaOrg());
        equipConcentSaveDTO.setIsVirtual(equipConcentPO.getIsVirtual());
        equipConcentSaveDTO.setInstallationDate(equipConcentPO.getInstallationDate());
        equipConcentSaveDTO.setHandlingDate(equipConcentPO.getHandlingDate());
        equipConcentSaveDTO.setSysconfigSupplier(equipConcentPO.getSysconfigSupplier());
        equipConcentSaveDTO.setSysconfigEquipmentModel(equipConcentPO.getSysconfigEquipmentModel());
        equipConcentSaveDTO.setEquipmentAgree(equipConcentPO.getEquipmentAgree());
        equipConcentSaveDTO.setEquipmentState(equipConcentPO.getEquipmentState());
        equipConcentSaveDTO.setEquipmentUpside(equipConcentPO.getEquipmentUpside());
        equipConcentSaveDTO.setEquipmentDownside(equipConcentPO.getEquipmentDownside());
        equipConcentSaveDTO.setEquipmentConnectType(equipConcentPO.getEquipmentConnectType());
        equipConcentSaveDTO.setNormalDayDosage(equipConcentPO.getNormalDayDosage());

        return equipConcentSaveDTO;
    }

    @Override
    public EquipConcentPO getPO(Integer id) {
        EquipConcentPO equipConcentPO = equipConcentPOMapper.selectById(id);
        if(equipConcentPO==null||SystemConsts.BooleanEnum.TRUE.value.equals(equipConcentPO.getIsDel())){
            throw new MyException(ResponseCode.RECORD_NOT_EXIST);
        }
        return equipConcentPO;
    }

    @Override
    public Map<String, List<EnumVO>> getEnums() {
        Map<String, List<EnumVO>> resultMap = new HashMap<>();

        List<EnumVO> booleanInt = new ArrayList<>();
        for (SystemConsts.BooleanIntEnum value : SystemConsts.BooleanIntEnum.values()) {
            EnumVO vo = new EnumVO();
            vo.setValue(value.value);
            vo.setValueName(value.valueName);
            booleanInt.add(vo);
        }

        List<EnumVO> equipmentAgree = new ArrayList<>();
        for (SystemConsts.EquipmentAgreeEnum value : SystemConsts.EquipmentAgreeEnum.values()) {
            EnumVO vo = new EnumVO();
            vo.setValue(value.value);
            vo.setValueName(value.valueName);
            equipmentAgree.add(vo);
        }

        List<EnumVO> runState = new ArrayList<>();
        for (SystemConsts.RunStateEnum value : SystemConsts.RunStateEnum.values()) {
            EnumVO vo = new EnumVO();
            vo.setValue(value.value);
            vo.setValueName(value.valueName);
            runState.add(vo);
        }

        List<EnumVO> equipmentUpside = new ArrayList<>();
        for (SystemConsts.EquipmentUpsideEnum value : SystemConsts.EquipmentUpsideEnum.values()) {
            EnumVO vo = new EnumVO();
            vo.setValue(value.value);
            vo.setValueName(value.valueName);
            equipmentUpside.add(vo);
        }

        List<EnumVO> equipmentDown = new ArrayList<>();
        for (SystemConsts.EquipmentDownEnum value : SystemConsts.EquipmentDownEnum.values()) {
            EnumVO vo = new EnumVO();
            vo.setValue(value.value);
            vo.setValueName(value.valueName);
            equipmentDown.add(vo);
        }

        List<EnumVO> equipmentConnect = new ArrayList<>();
        for (SystemConsts.EquipmentConnectType value : SystemConsts.EquipmentConnectType.values()) {
            EnumVO vo = new EnumVO();
            vo.setValue(value.value);
            vo.setValueName(value.valueName);
            equipmentConnect.add(vo);
        }

        resultMap.put("booleanInt", booleanInt);
        resultMap.put("equipmentAgree", equipmentAgree);
        resultMap.put("runState", runState);
        resultMap.put("equipmentUpside", equipmentUpside);
        resultMap.put("equipmentDown", equipmentDown);
        resultMap.put("equipmentConnect", equipmentConnect);
        return resultMap;
    }
}
